let app = getApp()
Page({
    data: {
        isLogin: false,
        userInfo: {},
        pm: undefined
    },
    // 页面刷新
    onShow() {
        let miniUserInfo = wx.getStorageSync('mini-userInfo')
        if (miniUserInfo) {
            this.setData({
                isLogin: true
            })
            this.setData({
                userInfo: miniUserInfo,
            })
            this.getRanking()
        } else {
            wx.showModal({
                title: '提示',
                content: '还未登录，请先登录！',
                showCancel: false,
                success: (res) => {
                    if (res.confirm) {
                        app.$util.switchTabTo("/pages/user/user")
                    }
                }
            })
            this.setData({
                isLogin: false
            })
        }
    },
    //获取排名
    getRanking() {
        let than = this
        wx.showLoading({
            title: '加载中...',
        })
        wx.request({
            url: 'http://localhost:8080/api/rank/list',
            method: 'GET',
            success(res) {
                let userInfo = wx.getStorageSync('mini-userInfo')
                let index = res.data.findIndex((item) => {
                    return item.nickName == userInfo.nickName
                })
                setTimeout(() => {
                    // 排名数据
                    than.setData({
                        pm:index+1
                    })
                    wx.hideLoading()
                }, 500)
            }
        })
    },
    // 去答题
    toAnswer(e) {
        let {
            page
        } = e.currentTarget.dataset
        app.$util.navigateTo(`/pages/answer/${page}/${page}`)
    },
    // 去错题
    goWrongList() {
        wx.navigateTo({
            url: '/pages/wrongList/wrongList',
        })
    }
})